//
//  AppDelegate.m
//  PersonalOCTest
//
//  Created by zlf on 2025/4/16.
//

#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()
@property (nonatomic, assign, readonly) BOOL isDeviceLocked;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    // 创建window
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    
    // 初始化你的根视图控制器
    ViewController *rootViewController = [[ViewController alloc] init];
    
    // 设置导航控制器（如果需要的话）
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    
    // 设置window的根视图控制器
    self.window.rootViewController = navigationController; // 或者直接使用 rootViewController 如果不需要导航栏
    
    // 显示window
    [self.window makeKeyAndVisible];
    
    [self checkInitialProtectedDataState];
    [self setupProtectedDataNotifications];
    return YES;
}

- (void)setupProtectedDataNotifications {
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    
    // 监听受保护数据变为可用（解锁）
    [center addObserver:self
               selector:@selector(protectedDataDidBecomeAvailable:)
                   name:UIApplicationProtectedDataDidBecomeAvailable
                 object:nil];
    
    // 监听受保护数据即将不可用（锁屏）
    [center addObserver:self
               selector:@selector(protectedDataWillBecomeUnavailable:)
                   name:UIApplicationProtectedDataWillBecomeUnavailable
                 object:nil];
}

- (void)protectedDataDidBecomeAvailable:(NSNotification *)notification {
    NSLog(@"受保护数据可用 - 设备已解锁");
    
}

- (void)protectedDataWillBecomeUnavailable:(NSNotification *)notification {
    NSLog(@"受保护数据即将不可用 - 设备即将锁屏");
    
}
- (void)checkInitialProtectedDataState {
    // 检查当前受保护数据是否可用
    BOOL isProtectedDataAvailable = [UIApplication sharedApplication].isProtectedDataAvailable;
    _isDeviceLocked = !isProtectedDataAvailable;
    
    NSLog(@"初始状态 - 受保护数据可用: %@, 设备锁定: %@",
          isProtectedDataAvailable ? @"是" : @"否",
          _isDeviceLocked ? @"是" : @"否");
}

@end
